/*
 * Created on Jun 6, 2010
 */
package ro.letsdoitromania;

import java.util.Date;
import javax.microedition.lcdui.Image;
import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;
import com.mgmaps.app.APPPlace;
import com.mgmaps.tools.UITools;
import com.mgmaps.utils.ExtLogger;
import com.mgmaps.utils.LS;
import com.nutiteq.components.Place;
import com.nutiteq.components.WgsPoint;

/**
 * Class LDIRPlace description.
 *
 * @author CristiS
 * @version 1.0
 */
public class LDIRPlace extends APPPlace {
  
  public static final int PT_TRASHPOINT = 1;
  public static final int PT_CLUSTER = 2;
  public static final int PT_COLLECTIONBASE = 3;
  
  private static final int PS_SMALL = 0;
  private static final int PS_MEDIUM = 1;
  private static final int PS_LARGE = 2;
  
  private TrashPointData data;
  private int type;
  private int iconNum;
  private String[] photos;
  private int clusterCount;
  
  public static Image[][] smileys;
  public static Image clusterIcon;
  private static String[] smileyImageNames = { "16", "24", "32" };
  
  private static int nextID = 1000000;
  
  public static void loadIcons() {
    try {
      smileys = new Image[3][7];
      for (int i=0; i<3; i++) {
        final Image smileyImage = Image.createImage("/img/i" + smileyImageNames[i] + ".png");
        for (int j=0; j<7; j++)
          smileys[i][j] = UITools.extractImage(smileyImage, 7, j);
      }
      
      clusterIcon = Image.createImage("/img/icl.png");
    }
    catch (Exception ex) {
      ExtLogger.handle(ex, true, "Error loading place icons");
    }
  }
  
  /**
   * Load from predefined data.
   *
   * @param data saved data (incl. place photos?)
   */
  public LDIRPlace(TrashPointData data) {
    super();
    this.data = data;
    loadPlaceFromData();
  }

  /**
   * Load from a JSON object (type = trashpoint, cluster, etc).
   *
   * @param json JSON object
   */
  public LDIRPlace(JSONObject jo) throws JSONException {
    id = jo.getInt("id");
    type = jo.getInt("type");
    double lat = jo.getDouble("lat");
    double lng = jo.getDouble("lng");
    WgsPoint p = new WgsPoint(lng, lat);
    
    if (type == PT_TRASHPOINT) {
      String desc = jo.getString("desc");
      int tm = jo.getInt("tm");
      double dl = jo.getDouble("dl");
      double dw = jo.getDouble("dw");
      double dh = jo.getDouble("dh");
      boolean dif = jo.getBoolean("dif");
      boolean difRain = jo.getBoolean("difr");
      boolean disp = jo.getBoolean("disp");
      double bageq = jo.getDouble("bageq");
      iconNum = jo.getInt("ic");
      JSONArray percJ = jo.getJSONArray("perc");
      int pjl = percJ.length();
      int[] perc = new int[pjl];
      for (int i=0; i<pjl; i++)
        perc[i] = percJ.getInt(i);
      data = new TrashPointData(id, new Date((long) tm * 1000), p, desc, dl, dw, dh, dif, difRain, disp, bageq, perc);
      loadPlaceFromData();
      
      JSONArray photosJ = jo.getJSONArray("ph");
      int pjm = photosJ.length();
      photos = new String[pjm];
      for (int i=0; i<pjm; i++)
        photos[i] = photosJ.getString(i);
    }
    
    else if (type == PT_CLUSTER) {
      clusterCount = jo.getInt("num");
      name = LS.get("ldir.place.clustercount", new String[] { Integer.toString(clusterCount) });
      place = new Place(++nextID, name, clusterIcon, p);
    }
    
    else if (type == PT_COLLECTIONBASE) {
      // TODO collection base attributes
    }
    
    // TODO other types
  }
  
  private void loadPlaceFromData() {
    id = data.getID();
    type = PT_TRASHPOINT;
    // make sure we have different IDs for different place types
    // implemented on the server
    name = "";
    final String desc = data.getDescription();
    if (desc != null)
      name += desc;
    name += " (" + id + ")";
    int sz = getRelativeSize();
    place = new Place(id, name, smileys[sz][iconNum], data.getPoint());
  }
  
  private int getRelativeSize() {
    double bageq = data.getBagEq();
    return (bageq <= 10) ? PS_SMALL : (bageq <= 100) ? PS_MEDIUM : PS_LARGE;
  }

  /**
   * @return x anchor
   * @see com.mgmaps.app.APPPlace#getAnchorX()
   */
  public int getAnchorX() {
    if (type == PT_CLUSTER)
      return 0;
    else
      return getIconWidth()>>1;
  }

  /**
   * @return y anchor
   * @see com.mgmaps.app.APPPlace#getAnchorY()
   */
  public int getAnchorY() {
    if (type == PT_CLUSTER)
      return getIconHeight()-1;
    else
      return getIconHeight()>>1;
  }

  /**
   * @return the icon for this place
   * @see com.mgmaps.app.APPPlace#getIcon()
   */
  public Image getIcon() {
    return place.getIcon();
  }

  /**
   * @return icon height
   * @see com.mgmaps.app.APPPlace#getIconHeight()
   */
  public int getIconHeight() {
    return getIcon().getHeight();
  }

  /**
   * @return icon width
   * @see com.mgmaps.app.APPPlace#getIconWidth()
   */
  public int getIconWidth() {
    return getIcon().getWidth();
  }

  /**
   * Whether this place is shown on the map.
   * @return true
   * @see com.mgmaps.app.APPPlace#onMap()
   */
  public boolean onMap() {
    return true;
  }

  public int getType() {
    return type;
  }

  public TrashPointData getData() {
    return data;
  }

  public String[] getPhotos() {
    return photos;
  }
}
